/*
人员 csy 2020年10月16日10:36:10
*/
<template>
    <div>
        <div class="container">
            <div class="left-content">
                <el-input placeholder="输入关键字进行过滤" v-model="filterText"></el-input>
                <el-tree
                    class="filter-tree"
                    :data="treeData"
                    :props="defaultProps"
                    default-expand-all
                    draggable
                    :filter-node-method="filterNode"
                    @node-click="onNodeClick"
                    :highlight-current="true"
                    ref="tree"
                ></el-tree>
            </div>
            <div class="right-content">
                <div class="handle-box">
                    <el-input
                        v-model="condition.name"
                        placeholder="人员名称"
                        class="handle-select mr10"
                    ></el-input>
                    <el-input
                        v-model="condition.code"
                        placeholder="人员编码"
                        class="handle-select mr10"
                    ></el-input>
                    <el-select
                        v-model="condition.status"
                        placeholder="请选择状态"
                        class="handle-select mr10"
                    >
                        <el-option key="1" label="在职" value="1"></el-option>
                        <el-option key="2" label="离职" value="0"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="getHrPerson"></el-button>
                    <el-button type="primary" @click="onOpenPersonDig">新增</el-button>
                    <el-button type="primary" @click="handleEdit">修改</el-button>
                    <el-button type="primary" @click="onDel">删除</el-button>
                </div>
                <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
                    @row-click="clickRow"
                    @row-dblclick="dblclickRow"
                >
                    <el-table-column prop="id" label="#" width="55" align="center"></el-table-column>
                    <el-table-column type="selection" width="55" align="center"></el-table-column>
                    <el-table-column prop="name" width="80" label="人员名称" align="center"></el-table-column>
                    <el-table-column prop="code" width="80" label="人员编码" align="center"></el-table-column>
                    <el-table-column prop="sex" width="50" label="性别" align="center"></el-table-column>
                    <el-table-column prop="nation" width="50" label="民族" align="center"></el-table-column>
                    <el-table-column prop="marriage" width="50" label="婚否" align="center"></el-table-column>
                    <el-table-column prop="affiliation" width="100" label="政治面貌" align="center"></el-table-column>
                    <el-table-column prop="iDCard" width="240" label="身份证号" align="center"></el-table-column>
                    <el-table-column prop="birthDay" width="120" label="出生日期" align="center"></el-table-column>
                    <el-table-column prop="telphone" width="120" label="电话" align="center"></el-table-column>
                    <el-table-column prop="moiblePhone" width="120" label="手机" align="center"></el-table-column>
                    <el-table-column prop="email" width="150" label="邮箱" align="center"></el-table-column>
                    <el-table-column prop="address" label="现居地址" align="center"></el-table-column>
                    <el-table-column prop="hometown" width="80" label="籍贯" align="center"></el-table-column>
                    <el-table-column prop="degree" width="80" label="学历" align="center"></el-table-column>
                    <el-table-column label="状态" width="80" align="center">
                        <template slot-scope="scope">
                            <el-tag v-show="scope.row.status===1">在职</el-tag>
                            <el-tag v-show="scope.row.status===0" type="warning">离职</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="remark" label="备注" align="center"></el-table-column>
                </el-table>
                <div class="pagination">
                    <el-pagination
                        background
                        layout="total, sizes, prev, pager, next, jumper"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :page-sizes="[50, 100, 200, 400]"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                        @size-change="handleSizeChange"
                    ></el-pagination>
                </div>
            </div>
        </div>
        <!-- 编辑弹出框 -->
        <hrPersonUpdate ref="hrPersonUpdate" @renderTable="getHrPerson" />
    </div>
</template>

<script>
import { findHrOrgan, findHrPerson, deleteHrPerson } from '@/api/hrPerson';
import hrPersonUpdate from '@/components/views/hrPerson/hrPersonUpdate';
export default {
    name: 'basetable',
    components: {
        hrPersonUpdate
    },
    data() {
        return {
            condition: {
                status: '',
                departmentId: '',
                code: '',
                name: ''
            },
            query: {
                page: 1,
                limit: 50
            },
            pageTotal: 0,
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            form: {},
            idx: -1,
            id: 1,
            filterText: '',
            treeData: [],
            defaultProps: {
                children: 'children',
                label: 'title'
            }
        };
    },
    created() {
        this.getData();
        this.getHrPerson();
    },
    methods: {
        // 查询树
        getData() {
            findHrOrgan().then((res) => {
                let obj = [
                    {
                        id: 1,
                        title: '机构',
                        children: res
                    }
                ];
                this.treeData = obj;
                console.log('查询树', res);
                console.log('defaultProps', this.defaultProps);
            });
        },
        // 查询人员
        getHrPerson() {
            console.log('query', this.query);
            this.query.hrPerson = JSON.stringify(this.condition);
            findHrPerson(this.query).then((res) => {
                console.log('查询人员', res);
                this.tableData = res.data;
                this.pageTotal = res.count;
            });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        // 新增按钮
        onOpenPersonDig() {
            // this.idx = index;
            // this.form = row;
            this.condition.departmentId != ''
                ? this.$refs.hrPersonUpdate.show(this.condition.departmentId)
                : this.$message({ message: '请先选择机构！', type: 'warning' });
        },
        // 编辑操作
        handleEdit() {
            if (this.multipleSelection.length === 0) {
                this.$message({ message: '请先选择机构！', type: 'warning' });
            } else if (this.multipleSelection.length > 1) {
                this.$message({ message: '只能选择一条数据', type: 'warning' });
            } else {
                this.$refs.hrPersonUpdate.updateShow(this.multipleSelection[0]);
            }
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
        // 删行
        onDel() {
            if (this.multipleSelection.length === 0) {
                this.$message({
                    message: '请选择要删除的数据',
                    type: 'warning'
                });
            } else {
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        console.log('then');
                        let arr = [];
                        this.multipleSelection.forEach((ele) => arr.push(ele.id));
                        console.log('arr', arr);
                        let condition = {};
                        condition.ids = arr.join(',');
                        console.log('ids', condition);
                        deleteHrPerson(condition)
                            .then((res) => {
                                console.log('res  del', res);
                                if (res.success === true) {
                                    this.$message({
                                        message: res.info,
                                        type: 'success'
                                    });
                                    this.query.page = 1;
                                    this.getHrPerson();
                                } else {
                                    this.$message({
                                        message: res.info,
                                        type: 'warning'
                                    });
                                }
                            })
                            .catch(() => {});
                    })
                    .catch(() => {
                        console.log('cancel');
                    });
            }
        },
        // 树查询
        filterNode(value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        },
        // 单击行事件
        clickRow(row) {
            this.$refs.multipleTable.toggleRowSelection(row);
        },
        // 双击行事件
        dblclickRow(row, column, event) {
            console.log('dblclickRow', row, column, event);
            // this.handleEdit();
            this.$refs.hrPersonUpdate.updateShow(row);
        },
        // 树节点点击
        onNodeClick(data) {
            console.log('onNodeClick data', data);
            this.condition.departmentId = data.id;
            this.getHrPerson();
        },
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
        }
    },
    watch: {
        filterText(val) {
            this.$refs.tree.filter(val);
        }
    }
};
</script>

<style scoped lang="scss">
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
.container {
    display: flex;
    justify-content: left;
    .left-content {
        width: 20%;
        height: 100%;
        margin-right: 5px;
    }
    .right-content {
        width: 80%;
        height: 100%;
    }
}
</style>
